package com.mes.web.controller.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import com.mes.biz.domain.*;
import com.mes.biz.service.BizInventoryOpeningDetailsService;
import com.mes.biz.service.BizProductService;
import com.mes.biz.service.BizWarehouseService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.service.BizInventoryOpeningService;

/**
 * 库存期初Controller
 * @author wy
 * @email wy
 * @date 2023-06-08
 */
@RestController
@RequestMapping("/biz/bizInventoryOpening")
public class BizInventoryOpeningController extends BaseController {

    @Autowired
    private BizInventoryOpeningService bizInventoryOpeningService;

    @Resource
    private BizInventoryOpeningDetailsService bizInventoryOpeningDetailsService;

    @Resource
    private BizWarehouseService bizWarehouseService;

    @Resource
    private BizProductService bizProductService;

    /**
     * 查询库存期初列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOpening:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizInventoryOpening bizInventoryOpening, HttpServletRequest request, HttpServletResponse response) {
        bizInventoryOpening.setPage(new PageDomain(request, response));
        PageInfo<BizInventoryOpening> page = bizInventoryOpeningService.findPage(bizInventoryOpening);
        for (BizInventoryOpening inventoryOpening : page.getList()) {
            if(StringUtils.isNotEmpty(inventoryOpening.getWarehouseId())) {
                BizWarehouse warehouse = bizWarehouseService.get(inventoryOpening.getWarehouseId());
                if(null!=warehouse) {
                    inventoryOpening.setWarehouseName(warehouse.getWarehouseName());
                }
            }
        }
        return R.data(page);
    }

    /**
     * 获取库存期初详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOpening:query')")
    @GetMapping(value = "/{id}")
    public R<BizInventoryOpening> detail(@PathVariable("id") String id) {
        BizInventoryOpening bizInventoryOpening = bizInventoryOpeningService.get(id);
//        BizInventoryOpeningDetails paramDetail = new BizInventoryOpeningDetails();
//        paramDetail.setOrderNo(bizInventoryOpening.getOrderNo());
        List<BizInventoryOpeningDetails> list = bizInventoryOpeningDetailsService.getListByOrderNo(bizInventoryOpening.getOrderNo());
        for (BizInventoryOpeningDetails inventoryOpeningDetails : list) {
//            BizProduct bizProduct = bizProductService.getBizProductByProductCode(inventoryOpeningDetails.getProductCode());
//            inventoryOpeningDetails.setBrand(bizProduct.getBrand());
//            inventoryOpeningDetails.setSpec(bizProduct.getSpec());
//            inventoryOpeningDetails.setColor(bizProduct.getColor());
//            inventoryOpeningDetails.setPurchasesPrice(bizProduct.getPurchasesPrice());
//            inventoryOpeningDetails.setPurchasesPriceA(bizProduct.getPurchasesPriceA());
//            inventoryOpeningDetails.setPurchasesPriceB(bizProduct.getPurchasesPriceB());
            List<Map<String,Object>> utilList = new ArrayList<>();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("key",1);
            map.put("name",inventoryOpeningDetails.getAssistUnit());
            utilList.add(map);
            map = new HashMap<String,Object>();
            map.put("key",2);
            map.put("name",inventoryOpeningDetails.getAssistUnitA());
            utilList.add(map);
            map = new HashMap<String,Object>();
            map.put("key",3);
            map.put("name",inventoryOpeningDetails.getAssistUnitB());
            utilList.add(map);
            inventoryOpeningDetails.setUnitList(utilList);
        }

        bizInventoryOpening.setDetailsList(list);
        return R.data(bizInventoryOpening);
    }

    /**
     * 新增库存期初
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOpening:add')")
    @Log(title = "库存期初", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizInventoryOpening bizInventoryOpening) {
        return R.status(bizInventoryOpeningService.insertBizInventoryOpening(bizInventoryOpening));
    }

    /**
     * 修改库存期初
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOpening:edit')")
    @Log(title = "库存期初", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizInventoryOpening bizInventoryOpening) {
        return R.status(bizInventoryOpeningService.updateBizInventoryOpening(bizInventoryOpening));
    }

    /**
     * 删除库存期初
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOpening:remove')")
    @Log(title = "库存期初", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        if (StringUtils.isNotEmpty(ids)){
            for (String id : ids) {
                BizInventoryOpening bizInventoryOpening = bizInventoryOpeningService.get(id);
                if (bizInventoryOpening.getStatus() ==2){
                    return R.fail("待审核的订单才能删除");
                }
            }

        }
        return R.status(bizInventoryOpeningService.deleteBizInventoryOpeningByIds(ids));
    }


    /**
     * 导出库存期初列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizInventoryOpening:export')")
    @Log(title = "库存期初", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizInventoryOpening bizInventoryOpening) {
        List<BizInventoryOpening> list = bizInventoryOpeningService.findList(bizInventoryOpening);
        ExcelUtil<BizInventoryOpening> util = new ExcelUtil<BizInventoryOpening>(BizInventoryOpening.class);
        return util.exportExcel(list, "库存期初数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    /**
     * 审核
     */
    @GetMapping(value = "/audit/{id}")
    public R auditOrder(@PathVariable String id) {
        return R.status(bizInventoryOpeningService.auditBizInventoryOpening(id));
    }

    /**
     * 审核
     */
    @GetMapping(value = "/doAudit/{id}")
    public R doAuditOrder(@PathVariable String id) {
        return R.status(bizInventoryOpeningService.doAuditBizInventoryOpening(id));
    }

}
